package com.syxp.pda.action.standard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.Department;
import com.syxp.pda.entity.standard.DevAnswerList;
import com.syxp.pda.service.standard.DevAnswerService;

public class DevAnswerListAction extends BaseAction {
	@Resource
	private DevAnswerService devAnswerService;
	
	private int autoID;
	private DevAnswerList devAnswerList;
	
	public String loadQuestions() {
		List list = new ArrayList(); 
		try {
			QueryFilter filter = new QueryFilter() ;
			List<FieldBean> fields = new ArrayList<FieldBean>();
			fields.add(new FieldBean("alCode",null,"DISTINCT"));
			fields.add(new FieldBean("question",null,null));
			filter.addProject(fields) ;
			List devAnswerLists = this.devAnswerService.getDao().getAll(filter) ;
//			for(DevAnswerList o: devAnswerLists) {
//				Map map = new HashMap();
//				map.put("alCode", o.getAlCode());
//				map.put("question", o.getQuestion());
//				list.add(map);
//			}
			for(Object[] o: (List<Object[]>)devAnswerLists) {
				Map map = new HashMap();
				map.put("alCode", o[0]);
				map.put("question", o[1]);
				list.add(map);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		this.jsonString = this.gson.toJson(list);
		System.out.println(this.jsonString);
		return SUCCESS;
	}
	
	public String load() {
		List list = new ArrayList(); 
		try {
			List<DevAnswerList> devAnswerLists = this.devAnswerService.getDao().getAll();
			for(DevAnswerList o: devAnswerLists) {
				Map map = new HashMap();
				map.put("autoID", o.getAutoID());
				map.put("alCode", o.getAlCode());
				map.put("question", o.getQuestion());
				map.put("answer", o.getAnswer());
				map.put("bugLevelID", o.getBugLevel().getBugLevel());
				map.put("bugLevelName", o.getBugLevel().getBugLevelName());
				list.add(map);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		this.jsonString = this.gson.toJson(list);
		System.out.println(this.jsonString);
		return SUCCESS;
	}
	
	public String save() {
		try {
			this.devAnswerService.addService(this.devAnswerList);
			this.jsonString="{success:true}" ;
		} catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS;
	}
	
	public String delete() {
		try {
			this.devAnswerService.removeService(this.autoID);
			this.jsonString="{success:true}" ;
		} catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS;
	}
	
	public String update() {
		try {
			this.devAnswerService.updateService(this.devAnswerList);
			this.jsonString="{success:true}" ;
		} catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS;
	}

	/**
	 * @return the devAnswerService
	 */
	public DevAnswerService getDevAnswerService() {
		return devAnswerService;
	}

	/**
	 * @param devAnswerService the devAnswerService to set
	 */
	public void setDevAnswerService(DevAnswerService devAnswerService) {
		this.devAnswerService = devAnswerService;
	}

	/**
	 * @return the autoID
	 */
	public int getAutoID() {
		return autoID;
	}

	/**
	 * @param autoID the autoID to set
	 */
	public void setAutoID(int autoID) {
		this.autoID = autoID;
	}

	/**
	 * @return the devAnswerList
	 */
	public DevAnswerList getDevAnswerList() {
		return devAnswerList;
	}

	/**
	 * @param devAnswerList the devAnswerList to set
	 */
	public void setDevAnswerList(DevAnswerList devAnswerList) {
		this.devAnswerList = devAnswerList;
	}
}
